home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4662 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: news.uni-jena.de!news
  2. From: mkt@isun04.inf.uni-jena.de (Tilo Koerbs)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: delete[] and template questions
  5. Date: 31 Jan 1996 14:35:05 GMT
  6. Organization: Lehrstuhl fuer Rechnerarchitektur- und kommunikation, FSU Jena
  7. Message-ID: <4enump$qlr@fsuj01.rz.uni-jena.de>
  8. References: <1996Jan30.165654.2033@iiasa.ac.at>
  9. Reply-To: mkt@isun04.inf.uni-jena.de
  10. NNTP-Posting-Host: isun07.inf.uni-jena.de
  11.  
  12. > template <class I, class T>
  13. > void mVect<I,T>::resize(I new_size) {
  14. >   T *old = v;
  15. >   v = new T[new_size];        // T *v is a private member of mVect
  16. >    int size_of_elem = sizeof(T);        // <--- question 3
  17. >   delete[] old;        // <--- questions 1 & 2
  18. > }
  19. >
  20. > I have the following questions:
  21. > 1. Is it absolutely robust and portable to delete[] old, even
  22. >   if a previous call was for new_size == 0 (or if v was allocated
  23. >     by the ctor for the size == 0) ?
  24. > 2. Is it correct to assume that no destructor is called by this statement ?
  25. > 3. Is there any risk involved in using sizeof(T) in this statement ?
  26.  
  27. 1. If you really called new T[x] regardless if x == 0,
  28.     or if old is a NULL pointer
  29.     than the answer is YES!
  30. 2. For every object created by the corresponding 'new' the destructor
  31.     is called there. (For x = new T[10]; on delete [] x; 10 constructors
  32.     will be called.)
  33.     If zero objects were created (new T[0]) so no destructor is called!
  34. 3. NO!
  35.     There is no risk at all. When the template is intantiated
  36.     the size of T is known! Otherwise the statement new T[..]
  37.     cannot be compiled!
  38.     
  39.  
  40.